<template>
	<view class="content">
		<!--structure 1-->
		<monthly-grade v-if="structure" :card-data="cardData" :loading="loading" @click="handleBtnClick"></monthly-grade>
		<monthly-fixed v-else :card-data="cardData" :loading="loading" @click="handleBtnClick"></monthly-fixed>
		<!--structure 2-->
		
		<view class="bottom-safe-area"></view>
		<!--单悬浮客服-->
		<common-drag />
	</view>
</template>
<script>
	import monthlyGrade from './components/monthly-grade.vue';
	import monthlyFixed from './components/monthly-fixed.vue';
	export default {
		components:{
			monthlyGrade,
			monthlyFixed
		},
		data() {
			return {
				structure:false,//控制月卡的ui展示是多功率还是单一功率
				id:'0',
				state:"1",//1普通购买月卡、2续费月卡、3升级月卡
				cardData:{},//main data
				loading:false,				
				from:'',//来源
			}
		},
		onLoad(options) {
			this.id=options.id;
			this.state=options.state;
			if(options.from){
				this.from=options.from;
			};
			this.getCardRequest();
		},
		methods: {
			//Get card details
			getCardRequest(){
				this.$u.api.getCardData({
					
					card_id:this.id,
					upgrade:this.state,
				}).then(res=>{
					if(res.result_code==300){
						this.cardData=res.data;
						//structure控制月卡的ui展示是多功率还是单一功率
						if(res.data.rank != 1){
							this.structure=true;
						}else{
							this.structure=false;
						};
						
					}else if(res.result_code==375){
						this.$utils.toast(res.msg);
					};
				}).catch(()=>{});
				
			},
			//pay
			handleBtnClick(index){
				this.loading=true;
				//1.wechat pay
				uni.login({
					provider: 'weixin',
					success:ret=>{
						//create order
						this.$u.api.commonPay({
							
							code:ret.code,
							member:5,
							card_id:this.id,
							cal_id:this.cardData.cal_id,
							upgrade:this.state,
							rank:this.cardData.list[index].uid,
							fee:this.cardData.upgrade == "3"?(this.cardData.list[index].this_pay-this.cardData.have_pay):(this.cardData.list[index].this_pay),
						}).then(res=>{
							
							if(res.result_code=='314'){
								let resOrderId=res.data.order_id;
								let tradeNO=res.data.trade_no;//支付宝支付需要的tradeNO
								if(resOrderId){
									
									uni.requestPayment({
										//微信
										// #ifdef	MP-WEIXIN
										provider: 'wxpay',
										timeStamp: res.data.timeStamp,
										nonceStr: res.data.nonceStr,
										package: res.data.package,
										signType: 'MD5',
										paySign: res.data.paySign,
										// #endif
										//支付宝
										// #ifdef	MP-ALIPAY
										provider: 'alipay',
										orderInfo:tradeNO,
										// #endif
										success: (resultData)=>{
											
											// #ifdef	MP-ALIPAY
											if(resultData?.resultCode==6001 || resultData?.resultCode==6002){
												//Payment failed
												this.$utils.modal({
													title:'提示',
													content:'支付失败',
													showCancel:false,
													callback:(bool)=>{
														this.loading=false;
													}
												});
												return false;
											};
											// #endif
											
											
											//Check if payment is successful
											this.$u.api.checkPaymentSuccess({
												order_id:resOrderId,
												
											}).then((retu)=>{
												if(retu.result_code=='300'){
													
													//payment successful
													let from=this.from;
													if(from=='1' || from=='2'){
														//Refresh core data in cache
														this.$u.api.portOpen({
															str:uni.getStorageSync('mainStr'),
															
														}).then(result=>{
															if(result.result_code=='300'){
																this.loading=false;
																//运维电话
																this.$u.vuex('maintain_phone',result.data.operator_phone);
																//更新
																uni.setStorage({
																	key: 'coreData',
																	data:result.data,
																	success:()=>{
																		//set isRefresh cache
																		uni.setStorageSync('isRefresh',1);
																		if(from=='1'){
																			this.$utils.back();
																		}else if(from=='2'){
																			this.$utils.back(2);
																		}
																		
																	}
																});
															}else{
																this.$utils.modal({
																	title:'提示',
																	content:result.msg,
																	showCancel:false,
																	callback:(bool)=>{
																		this.loading=false;
																	}
																});
															};
														}).catch(()=>{
															this.loading=false;
														});
													}else if(from=='3'){
														//关闭当前页面跳转至卡卷列表页面
														uni.redirectTo({
															 url:"/pages/member/card"
														});
														this.loading=false;
													};

												}else{
													this.$utils.modal({
														title:'提示',
														content:retu.msg,
														showCancel:false,
														callback:(bool)=>{
															this.loading=false;
														}
													});
												};
												
											}).catch(err=>{
												this.loading=false;
											});
				
										},
										fail:()=>{
											//Payment failed
											this.$utils.modal({
												title:'提示',
												content:'支付失败',
												showCancel:false,
												callback:(bool)=>{
													this.loading=false;
												}
											});
										}
									});
								};
							}else{
								this.$utils.modal({
									title:'提示',
									content:res.msg,
									showCancel:false,
									callback:(bool)=>{
										this.loading=false;
									}
								});

							};
						}).catch(()=>{
							
							this.loading=false;
						});
						
					}
				});
			},
		}
	}
</script>

<style lang="scss" scoped>
	.content{
		padding-bottom: 180rpx;
	}
</style>

